package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpShopSubjectDTO;
import com.financing.platform.service.IFpShopSubjectService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpShopSubjectController")
@RequestMapping("/api/fpShopSubject")
public class FpShopSubjectController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopSubjectService fpShopSubjectService;
	
	@RequestMapping("/listFpShopSubject.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopSubjectDTO fpShopSubject,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopSubject list begin .......");
			
			fpShopSubject.setBegin(pager.getStartIndex());
			fpShopSubject.setPageSize(pager.getPageSize());
			
			int count = fpShopSubjectService.queryListCount(fpShopSubject);
			
			pager.setTotalCount(count);
			
			List<FpShopSubjectDTO> fpShopSubjectList = null;
			
			if(count > 0){
				fpShopSubjectList = fpShopSubjectService.queryList(fpShopSubject);
			}else{
				fpShopSubjectList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopSubjectList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopSubject list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopSubject.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopSubject  begin .......");
			
			fpShopSubject.setDeleteTag("1");
			
			
			DTOdateSet.setCreateInfo(fpShopSubject, request.getSession());
			DTOdateSet.setUpdateInfo(fpShopSubject, request.getSession());
			
			
			int id = fpShopSubjectService.insert(fpShopSubject);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopSubject list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("add fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopSubject.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getShopSubjectId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopSubjectDTO dto = fpShopSubjectService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopSubject  end .......");
		}catch(Exception e){
			log.error("query detail fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopSubject.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getShopSubjectId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopSubjectService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopSubject  end .......");
		}catch(Exception e){
			log.error("delete fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopSubject.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopSubject  begin .......");
			DTOdateSet.setUpdateInfo(fpShopSubject, request.getSession());
			
			Integer id = fpShopSubject.getShopSubjectId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			fpShopSubjectService.update(fpShopSubject);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopSubject list end .......");
		}catch(Exception e){
			log.error("update fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopSubject.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopSubject  begin .......");
			DTOdateSet.setUpdateInfo(fpShopSubject, request.getSession());
			Integer id = fpShopSubject.getShopSubjectId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			fpShopSubjectService.updateForce(fpShopSubject);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopSubject list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
